import 'dart:ffi';

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
import 'package:search_people/page/home/main/views/hot_dynamic.dart';
import 'package:search_people/page/home/main/views/hot_people.dart';
import 'package:search_people/page/home/main/views/menu.dart';
import 'package:search_people/page/home/main/views/news_area.dart';
import 'package:search_people/page/home/main/views/people_news.dart';
import 'package:search_people/page/home/main/views/search.dart';
import 'package:search_people/page/home/main/views/title.dart';
import 'package:search_people/utils/img_util.dart';
import 'package:search_people/value/topcolors.dart';

import '../../../utils/component/component.dart';
import 'logic.dart';

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final logic = Get.put(HomeLogic());
    final state = Get.find<HomeLogic>().state;

    Widget _buildViews() {
      return NestedScrollView(
        headerSliverBuilder: ((context, innerBoxIsScrolled) {
          return [
            SliverToBoxAdapter(
              child: Column(
                children: [
                  Stack(
                    children: [
                      Image.asset(
                        'agl.png'.imgPathHome,
                        width: 1.sw,
                      ),
                      Column(
                        children: [
                          const HomeTitleWidget(),
                          HomeSearchItemWidget(),
                          const HotPeopleWidget(),
                        ],
                      )
                    ],
                  ),
                  HomeMenu(),
                  Container(
                    height: 12.r,
                    color: TopColors.bg,
                  )
                ],
              ),
            ),
            SliverPersistentHeader(
              pinned: true,
              floating: true,
              delegate: TPSliverPersistentHeaderDelegate(
                  floatColor: TopColors.main_color,
                  builder: (off) {
                    return SizedBox(
                      height: 50.r,
                      // color: Colors.white,
                      child: TPTabBar(
                        tabCtrl: state.tabCtrl,
                        tabTitles: state.tabTitles,
                        selectedColor:
                            off > 0 ? Colors.white : TopColors.main_color,
                        unselectedColor:
                            off > 0 ? Colors.white60 : TopColors.black_99,
                      ),
                    );
                  },
                  max: 50.r,
                  min: 50.r),
            ),
          ];
        }),
        body: TabBarView(
          controller: state.tabCtrl,
          children: [
            HotDynamicWidget(),
            PeopleNewsWidget(),
            NewsAreaWidget(),
          ],
        ),
      );
    }

    return Scaffold(
      backgroundColor: TopColors.main_color,
      body: Container(
          // color: TopColors.main_color,
          child: SafeArea(
        child: GetBuilder<HomeLogic>(
          builder: (controller) {
            return SmartRefresher(
              controller: state.rc!,
              onRefresh: () {
                Get.log('refresh');
              },
              enablePullDown: true,
              enablePullUp: true,
              child: _buildViews(),
            );
          },
        ),
      )),
    );
  }
}

class TPSliverPersistentHeaderDelegate extends SliverPersistentHeaderDelegate {
  final double max;
  final double min;
  final Color? floatColor;
  final Widget Function(double) builder;
  TPSliverPersistentHeaderDelegate({
    required this.builder,
    required this.max,
    required this.min,
    this.floatColor,
  });

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return Container(
      color: shrinkOffset > 0 ? floatColor : Colors.white,
      child: builder(shrinkOffset),
    );
  }

  @override
  double get maxExtent => max;

  @override
  double get minExtent => min;

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
    return false;
  }
}
